package sz.szsmk.citizencard.widget.lifeweb;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import sz.szsmk.citizencard.model.network.bean.JumpActivityEventBusBean;
import sz.szsmk.citizencard.util.JumpUtils;

public class JSInterface {

    public static volatile JSInterface jsInterface = null;
    public Context context;

    private JSInterface(Context context) {
        this.context=context;
    }

    public static JSInterface getInstance(Context context) {
        if (jsInterface == null) {
            synchronized (JSInterface.class) {
                if (jsInterface == null) {
                    jsInterface = new JSInterface(context);
                }
            }
        }
        return jsInterface;
    }

    //  wv_content.addJavascriptInterface(new JsInteration(), "android");
    //测试

    @JavascriptInterface
    public void launcherEventBus(String json) {
        Log.d("clark", "get web data"+json);
        try {
            JumpActivityEventBusBean jumpActivityBean = new Gson().fromJson(json, JumpActivityEventBusBean.class);
            if (jumpActivityBean.getCode() != 0) {
                EventBus.getDefault().post(jumpActivityBean);
            }
        } catch (Exception e) {
            Log.d("clark", "get web data error");
            Toast.makeText(context, "参数错误", Toast.LENGTH_SHORT).show();
        }
    }

    //客服电话
    @JavascriptInterface
    public void tell(final String arg) {
        {
            Intent phoneIntent = new Intent(Intent.ACTION_DIAL,
                    Uri.parse("tel:" + arg));
            context.startActivity(phoneIntent);
        }
    }

}
